import constants
from text import *
# 速度设置页面
def speedPage(screen) :
    Author = TEXT("author : blankspace", AuthorColor, AuthorSize)
    # 清图
    screen.fill(backgroundColor)
    Author.printText(AuthorPosition, screen)
    # 页面
    speedTitle = TEXT("Choose the speed you like", speedPageTitleColor, speedPageTitleSize)
    speed = []
    for i in range(7) : speed.append(TEXT("Level " + str(i), speedPageTextColor, speedPageTextSize))
    Exit = TEXT("exit", speedPageTextColor, speedPageTextSize)
    speedTip = TEXT("Different speed correspond to different scores", speedPageTextColor, tipSize)

    circlePosition = [[190, 215 + i * 60] for i in range(8)]
    speedPosition = [[210, 200 + i * 60] for i in range(8)]
    # 页面打印
    def printSpeed() :
        speedTitle.printText(speedPageTitlePosition, screen)
        for i in range(7) : speed[i].printText(speedPosition[i], screen)
        Exit.printText(speedPosition[7], screen)
        speedTip.printText(tipPosition, screen)
        pygame.display.update()
    # 速度颜色更新
    def showSpeed(k, hi) :
        if not hi :
            speed[k].changeColor(speedPageTextColor)
            speed[k].printText(speedPosition[k], screen)
        else : # 当前速度高亮
            speed[k].changeColor(highSpeedPageTextColor)
            speed[k].printText(speedPosition[k], screen)
        pygame.display.update() # 页面更新
    # 选项标识
    def drawCircle(k) :
        pygame.draw.circle(screen, circleColor, circlePosition[k], circleRadius)
        pygame.display.update()
    def clearCircle(k) :
        pygame.draw.circle(screen, backgroundColor, circlePosition[k], circleRadius)
        pygame.display.update()
    
    printSpeed()
    speedCirclePos = constants.snakerate
    showSpeed(speedCirclePos, True)
    drawCircle(speedCirclePos)
    lasthigh = constants.snakerate
    
    while True :
        for event in pygame.event.get() : # 键盘事件及处理
            if event.type == KEYDOWN :
                if event.key == K_UP or event.key == K_w:
                    Channel4.play(KeyBGM)
                    if speedCirclePos > 0 :
                        clearCircle(speedCirclePos)
                        speedCirclePos -= 1
                        drawCircle(speedCirclePos)
                if event.key == K_DOWN or event.key == K_s:
                    Channel4.play(KeyBGM)
                    if speedCirclePos < 7 :
                        clearCircle(speedCirclePos)
                        speedCirclePos += 1
                        drawCircle(speedCirclePos)
                if event.key == K_RETURN :
                    Channel4.play(KeyBGM)
                    if speedCirclePos >= 0 and speedCirclePos <= 6 :
                        showSpeed(lasthigh, False)
                        showSpeed(speedCirclePos, True)
                        lasthigh = speedCirclePos
                        constants.snakerate = speedCirclePos
                    elif speedCirclePos == 7 :
                        return 
